package com.imufe.chemicalenterprisedangerwarning.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.imufe.chemicalenterprisedangerwarning.entity.po.ProjectChemicalPO;
import com.imufe.chemicalenterprisedangerwarning.entity.po.ProjectPO;
import com.imufe.chemicalenterprisedangerwarning.entity.po.SafetySupervisionBureauPO;
import com.imufe.chemicalenterprisedangerwarning.entity.query.ProjectQuery;
import com.imufe.chemicalenterprisedangerwarning.entity.vo.ProjectChemicalDetailedVO;
import com.imufe.chemicalenterprisedangerwarning.entity.vo.ProjectDetailedVO;
import com.imufe.chemicalenterprisedangerwarning.mapper.ProjectMapper;
import com.imufe.chemicalenterprisedangerwarning.service.ProjectChemicalService;
import com.imufe.chemicalenterprisedangerwarning.service.ProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imufe.chemicalenterprisedangerwarning.util.BeanUtils;
import com.imufe.chemicalenterprisedangerwarning.util.ListUtils;
import jdk.jfr.Label;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author BaiBan
 * @since 2022-09-26
 */
@Log4j2
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, ProjectPO> implements ProjectService {

    @Autowired
    ProjectMapper mapper;

    @Autowired
    ProjectChemicalService projectChemicalService;

    @Override
    public ProjectDetailedVO detailed(Long id) {
        ProjectPO po = mapper.selectById(id);
        ProjectDetailedVO detailedVO = BeanUtils.copyPOJO(po, ProjectDetailedVO.class);
        log.info(detailedVO);

        // 组装化学品
        List<ProjectChemicalDetailedVO> chemicalList = projectChemicalService.chemicalList(id);
        detailedVO.setChemicals(chemicalList);

        log.info(detailedVO);
        return detailedVO;
    }

    @Override
    public List<ProjectDetailedVO> projectList(Long enterpriseId) {
        QueryWrapper<ProjectPO> qw = new QueryWrapper<>();
        qw.eq("enterprise_id", enterpriseId);
        List<ProjectPO> pos = mapper.selectList(qw);
        return pos.stream()
                .map(po -> this.detailed(po.getId()))
                .toList();
    }

    @Override
    public ProjectDetailedVO create(ProjectQuery query) {
        ProjectPO po = BeanUtils.copyPOJO(query, ProjectPO.class);
        log.info(po);
        mapper.insert(po);
        return this.detailed(po.getId());
    }

    @Override
    public ProjectDetailedVO update(ProjectQuery query) {
        ProjectPO po = BeanUtils.copyPOJO(query, ProjectPO.class);
        log.info(po);
        mapper.updateById(po);
        return this.detailed(po.getId());
    }
}
